package com.cyj.crm02.service.impl;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyj.crm02.entity.Employee;
import com.cyj.crm02.entity.EmployeeRole;
import com.cyj.crm02.mapper.EmployeeMapper;
import com.cyj.crm02.service.EmployeeRoleService;
import com.cyj.crm02.service.EmployeeService;
import com.cyj.crm02.utils.SecurityUtils;
import com.cyj.crm02.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee>  implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private EmployeeRoleService employeeRoleService;

    @Override
    public Employee selectByEmployeeName(String employeeName) {
        return employeeMapper.selectByEmployeeName(employeeName);
    }

    @Override
    public IPage<Employee> selectEmployeeList(Page<Employee> page) {
        return employeeMapper.selectEmployeeList(page);
    }

    @Override
    public Integer addEmployee(Employee employee, Integer roleId) {
        System.out.println("添加方法的参数：employee:"+employee);
        System.out.println("添加方法的参数：roleId:"+roleId);
        /*
            1.获取盐
            2.shiro加盐加密
            3.用户信息存入对象，插入数据库，获取到插入的id
            4.将employeeId和roleId插入到role_employee表中
         */
        //从ShiroUtils类中随机生成盐
        employee.setSalt(SecurityUtils.randomSalt());
        //将密码设置为 加密后的密码（由ShiroUtils里面encryptPassword方法实现）
        employee.setPassword(SecurityUtils.encryptPassword(employee.getPassword(),employee.getCredentialsSalt()));
        //设置时间，idDel
        employee.setCreateTime(StringUtils.getNowTime());

        int employeeNum = employeeMapper.insert(employee);

        if (employeeNum==1){
//            QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
//
//            queryWrapper.eq("employee_name", employee.getEmployeeName());
//
//            Employee employee1  = employeeMapper.selectOne(queryWrapper);

            //将empId和roleId一同插入到  员工与角色关系表
            EmployeeRole employeeRole  = new EmployeeRole(employee.getEmployeeId(),roleId);
            employeeRole.setCreateTime(StringUtils.getNowTime());

            boolean flag = employeeRoleService.save(employeeRole);
            System.out.println("员工与角色关系表插入："+flag);
        }


        return employeeNum;
    }

    @Override
    public Integer update(Employee employee, Integer roleId) {
        System.out.println("员工信息参数："+employee);
        //判断用户是否输入密码，如果没有，获取的就是空字符串 ("")  就不修改密码
        if (!"".equals(employee.getPassword()) ){
            //从ShiroUtils类中随机生成盐
            employee.setSalt(SecurityUtils.randomSalt());
            //将密码设置为 加密后的密码（由ShiroUtils里面encryptPassword方法实现）
            employee.setPassword(SecurityUtils.encryptPassword(employee.getPassword(),employee.getCredentialsSalt()));
        }else {
            employee.setPassword(null);
        }

        employee.setUpdateTime(StringUtils.getNowTime());

        //将信息更新到数据库中（空的属性不修改）
        int result =  employeeMapper.updateById(employee);


        EmployeeRole employeeRole  = new EmployeeRole(employee.getEmployeeId(),roleId);

        AbstractWrapper wrapper = new QueryWrapper();
        wrapper.eq("employee_id",employee.getEmployeeId());
        employeeRoleService.update(employeeRole,wrapper);

        return result;
    }


}
